using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using DCI6.Client.API;
using DCI6;

namespace DCIAPIRMSampleCode {
    internal class DCIAPIRMSampleCode {
        private static void Main(string[] args) {
            try {
                //Create DDS Configuration
                DDSConfigurationSampleCode dDSConfigurationSampleCode = new DDSConfigurationSampleCode();
                DDSConfiguration config = dDSConfigurationSampleCode.CreateConfiguration();

                //Register with DCI
                // If you will be running multiple DCI Clients on one machine.
                // Please set DCIClient prefix and group ID before making any other
                // calls to the DCI API. DCI Client processes which set equal groupID
                // values will share the same Subscriptions, DDSEvents, DDSConfigurations,
                // DCI Validation Configurations, and all asynchronous call back data, 
                // between processes.
                DCIClient cl = new DCIClient("prefix", "groupID");

                //Configure DDS
                dDSConfigurationSampleCode.RegisterDDSConfiguration(config);

                //Create Advertisement
                AdvertisementSampleCode advertisementSampleCode = new AdvertisementSampleCode();
                Advertisement ad = advertisementSampleCode.CreateAdvertisement();
                advertisementSampleCode.Advertise(ad, config.ConnectionID);

                //Set Callback Handler
                ReceivingDataSampleCode receivingDataSampleCode = new ReceivingDataSampleCode();
                DCIMessageListener receiver = receivingDataSampleCode.ConstructCallBackListener();
                receivingDataSampleCode.SetCallBackListener(receiver);

                //Create Subscription
                //Subscription subscription = SubscriptionSampleCode.CreateSubscription();
                //SubscriptionSampleCode.Subscribe(subscription, config.ConnectionID);
                //Get simple sub for Callback data
                SubscriptionSampleCode subscriptionSampleCode = new SubscriptionSampleCode();
                Subscription subscription1 = subscriptionSampleCode.CreateSimpleSubForAdv(ad);
                subscriptionSampleCode.Subscribe(subscription1, config.ConnectionID);

                //Create Publication
                PublishingSampleCode publishingSampleCode = new PublishingSampleCode();
                Publication publication = publishingSampleCode.CreatePublication(ad);
                publishingSampleCode.Publish(publication, config.ConnectionID);

                //Query Data
                Thread.Sleep(3000);
                Console.WriteLine("Press ENTER to issue Query");
                Console.ReadLine();

                QuerySampleCode querySampleCode = new QuerySampleCode();
                QueryCommand qc = querySampleCode.CreateSimpleQueryCommandForAdv(ad);
                querySampleCode.ExecuteLocalSyncronousQuery(qc, config.ConnectionID);
                Thread.Sleep(2000);
                querySampleCode.ExecuteQuery(qc, config.ConnectionID);
                Thread.Sleep(2000);
                querySampleCode.ExecuteAdvanceQuery(qc, config.ConnectionID);
                Thread.Sleep(2000);
                Console.WriteLine("Press ENTER to cleanup");
                Console.ReadLine();

                //UnAdvertise
                advertisementSampleCode.UnAdvertise(ad, config.ConnectionID);
                Thread.Sleep(3000); // Should get retract callbacks

                //UnSubscribe
                subscriptionSampleCode.UnSubscribe(subscription1, config.ConnectionID);

                //StopCallbackListeners
                receivingDataSampleCode.StopCallBackListener();

                //Publish a C2R change and Listen for C2R changes for 10 seconds
                C2rChangesSampleCode c2r = new C2rChangesSampleCode(config.ConnectionID);
                Console.WriteLine("");
                Console.WriteLine("Publish a C2R change and Listen for C2R changes for 10 seconds...");
                Console.WriteLine("");
                c2r.testC2rChangeNotifier();
                c2r.tearDown();

                //Update the DDSConfiguration
                config.DdsSubscriberHost = "newHost";
                dDSConfigurationSampleCode.UpdateConfiguration(config);

                //Query Configurations then Remove
                dDSConfigurationSampleCode.QueryDDSConfigurations();
                dDSConfigurationSampleCode.DeleteConfiguration(config);
            }
            catch (DCIException ex) {
                Console.WriteLine("DCIException msg:" + ex.Message);
            }
            catch (Exception ex) {
                Console.WriteLine("Exception msg:" + ex.Message);
            }

            System.Environment.Exit(0);
        }
    }
}